package com.springmvc.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.springmvc.web.entity.LoginForm;
import com.springmvc.web.entity.User;
import com.springmvc.web.service.ILoginService;
import com.springmvc.web.service.IUserService;


@Controller
@RequestMapping("/Login")
public class LoginController extends BaseController{
	
		@Autowired
		private ILoginService loginService ;
		
		@Autowired
		private IUserService userService;
		
		
		@RequestMapping("/toLogin")
		public String toLogin(HttpSession session){
			
		    String usertype = (String)session.getAttribute("usertype");
			    
			   if(session.getAttribute("username") != null){
			    	
			    	if(usertype.equals("manager") == true){
			    		
			    		  return "AllUser";
			    	}else{
			    		  return "personalPage";
			    	  }
			      } 
			   
			   return "login";			    	 
		}
		
		@RequestMapping("/toRegister")
		public String toRegister(){
			return "register";
		}
		
	
		//登陆验证
		@RequestMapping("/doLogin")
		public String doLogin(HttpServletRequest request,HttpServletResponse response,LoginForm login,ModelMap modelMap,User user){
			try{
				if(StringUtils.isBlank(login.getUsername())){
					
					modelMap.put("error0", "用户名不能为空!");
				     
					return "login";
				}
				if(StringUtils.isBlank(login.getPassword())){
					
					modelMap.put("error0", "密码不能为空!");
					 
					return "login";
				}
	          				
		        LoginForm loginForm = this.loginService.getByUsername(login.getUsername());
			          HttpSession session = request.getSession();
				      session.setAttribute("username",login.getUsername());
				      session.setAttribute("password", login.getPassword());
				      session.setAttribute("usertype", login.getUsertype());
				 			          				   				   				      
				//用户名验证
				if(loginForm == null){
					
					modelMap.put("error0", "用户名不存在!");
					 
					return "login";
				}
				
			/*	 String username =(String)session.getAttribute("username");
			     if(username == loginForm.getUsername()){
			    	 
			    	  modelMap.put("error0", "该用户已经登录!");
						 
						return "login";  			    	  
			      }*/
				
				
				//验证密码
				if(!login.getPassword().equals(loginForm.getPassword())){
					
					modelMap.put("error0", "密码错误!");
					 
					return "login";
				}
				
				//验证用户类型
				if(!login.getUsertype().equals(loginForm.getUsertype())){
					
					modelMap.put("error0", "用户类型错误！");
					 
					return "login";
				}
								
				modelMap.put("loginform", login);
				
					}catch(Exception e){
					    e.printStackTrace();
				    }
			
			    
			
			
			if( login.getUsertype().equals("manager") == true){
			        
				      return "LoginSuccess";
			}else{
				      return "psLoginSuccess";
			}
			
			
			
		   }
		  
				
		   //注册验证
		   @RequestMapping("/add")
		   public String addLogin(HttpServletRequest request,ModelMap modelMap,LoginForm login,User user){
			   try {
				     if(StringUtils.isBlank(login.getUsername())){
				       
			  			modelMap.put("error", "用户名不能为空!");
					              
			  			return "register";
					}
				   if(StringUtils.isBlank(login.getPassword())){
					      
			  			modelMap.put("error", "密码不能为空!");
						     
						return "register";
					}			 
									   
				   LoginForm loginForm = this.loginService.getByUsername(login.getUsername());
				   
				   //验证用户名是否存在				
				   if(loginForm != null){
						 
						modelMap.put("error", "用户已存在!");
						
						return "register";
					}
				  
			      /* 
			       * 时间戳自动生成id 
			       * long id = System.currentTimeMillis();
			       * user.setId(String.valueOf(id));
			       */	
				  //创建用户
				  this.userService.addUser(user);
				 
				  //创建登陆信息
				  this.loginService.createLogin(login);	
				  
				 modelMap.put("loginform", login);
				  
	         } catch (Exception e) {
			   
	    	   e.printStackTrace();
			 }
			if( login.getUsertype().equals("manager") == true){
				
				      return "registerSuccess"; 				
			  }else{
				      return "psRegisterSuccess"; 
			  }	   
		     
	    }
	   
		
	   @RequestMapping("/logout")
	   public String logout(HttpSession session,HttpServletResponse response,HttpServletRequest request){
		   
		     session.removeAttribute("username");                
		     /* session.invalidate();*/            
             return "index";
	     }   
	   
		   
}
